package com.iris.live.services.common;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 */
public class DateUtils {
    private static final String[] months = {"一月","Jan","二月","Feb","三月","Mar","四月","Apr","五月","May","六月","Jun","七月","Jul","八月","Aug","九月","Sept","十月","Oct","十一月","Nov","十二月","Dec"};
    public static final String FULL_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String SIMPLE_FORMAT_N= "yyyy/MM/dd";
    public static final String SIMPLE_FORMAT = "yyyy-MM-dd";
    public static  final String TIME_FORMAT_STR_YYYYMMDD ="yyyyMMdd";
    public static  final String TIME_FORMAT_STR_YYYY_MM ="yyyy-MM";

    public static Date getCurrentDate(){
        return new Date();
    }

    public static Timestamp getCurrentTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }

    /**
     * 格式化日期
     * @param date
     * @return
     */
    public static String format(Date date) throws Exception{
        return format(date, FULL_FORMAT);
    }

    /**
     * 格式化日期
     * @param date
     * @param format
     * @return
     */
    public static String format(Date date, String format) throws Exception{
        if(date == null) return null;

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 字符串转日期
     * @param date
     * @return
     * @throws Exception
     */
    public static Date parse(String date) throws Exception{
        return parse(date, FULL_FORMAT);
    }

    /**
     * 字符串转日期
     * @param date
     * @param format
     * @return
     * @throws Exception
     */
    public static Date parse(String date, String format) throws Exception{
        if(date == null || date.trim().equals("")) return null;

        String[] dates = date.split(" ");
        String ymd = dates[0], times = "";
        if(dates.length > 1){
            times = dates[1];
        }

        if(ymd.contains("/")) {
            ymd = ymd.replaceAll("/", "-");
        }

        String[] values = ymd.split("-");
        String year = values[0], month = values[1], day = values[2];
        if (Integer.valueOf(values[0]) <= 31) {
            day = values[0];
            year = values[2];
        }else{
            day = values[2];
            year = values[0];
        }

        for(int index = 0; index < months.length; index++){
            if(months[index].trim().equalsIgnoreCase(values[1].trim())){
                month = index/2 < 9 ? "0" + (index/2+1):String.valueOf(index/2 + 1);
                break;
            }
        }
        date = String.format("%s-%s-%s %s", year, month, day, times).trim();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(date);
    }

    /**
     * 日期比较
     * @param source
     * @param target
     * @return
     * @throws Exception
     */
    public static int compare(Date source, Date target) throws Exception{
        if(source == null || target == null) return 9999;

        return source.compareTo(target);
    }

    /**
     * 间隔月份
     * @param paramInt1
     * @param paramInt2
     * @return
     */
    public static int monthsBetween(int paramInt1, int paramInt2) {
        int i = paramInt2 / 100 * 12 + paramInt2 % 100
                - (paramInt1 / 100 * 12 + paramInt1 % 100);
        return i;
    }

    /**
     *
     * @param paramDate1
     * @param paramDate2
     * @return
     */
    public static int monthsBetween(Date paramDate1,
                                    Date paramDate2) {
        return monthsBetween(getYM(paramDate1).intValue(), getYM(paramDate2).intValue());
    }


    /**
     * 间隔天数
     * @param paramDate1
     * @param paramDate2
     * @return
     */

    public static int daysBetween(Date paramDate1,
                                    Date paramDate2) {
        if(paramDate1 == null || paramDate2 == null) return 0;
        long start = paramDate1.getTime();
        long end = paramDate2.getTime();
        long num = (end-start)/(24 * 60 * 60 * 1000);
        return Integer.parseInt(String.valueOf(num));
    }




    /**
     * 日期加减
     * @param paramDate
     * @param paramInt
     * @return
     */
    public static Date addDate(Date paramDate, int type,
                                           int paramInt) {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        localCalendar.add(type, paramInt);

        return localCalendar.getTime();
    }

    /**
     *
     * @param paramDate
     * @return
     */
    private static Integer getYM(Date paramDate) {
        if (paramDate == null)
            return null;
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        int i = localCalendar.get(1);
        int j = localCalendar.get(2) + 1;
        return new Integer(i * 100 + j);
    }

    /**
     * 获取录入月份的第一天
     * @param day
     * @return
     */
    public static Date getDateByMonthFirstDay(Date day) throws Exception {
        Calendar calendar=new GregorianCalendar();
        SimpleDateFormat sdf=new SimpleDateFormat(DateUtils.FULL_FORMAT);
        calendar.setTime(parse(format(day,DateUtils.TIME_FORMAT_STR_YYYY_MM),DateUtils.TIME_FORMAT_STR_YYYY_MM));
        calendar.add(calendar.DATE, 0);//因为格式化时默认了DATE为本月第一天所以此处为0
        return parse(sdf.format(calendar.getTime()),DateUtils.FULL_FORMAT);
    }

    /**
     * 获取录入月份的最后一天
     * @param day
     * @return
     */
    public static Date getDateByMonthLastDay(Date day)  throws Exception {
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.SIMPLE_FORMAT);
        calendar.setTime(parse(format(day, DateUtils.TIME_FORMAT_STR_YYYY_MM), DateUtils.TIME_FORMAT_STR_YYYY_MM));
        calendar.roll(calendar.DATE, -1);//api解释roll()：向指定日历字段添加指定（有符号的）时间量，不更改更大的字段
        return parse(sdf.format(calendar.getTime()), DateUtils.FULL_FORMAT);
    }

    public static void main(String[] args) throws Exception{
        System.out.println(parse("9-May-2017", SIMPLE_FORMAT));
    }
}
